# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

# Remember to update mingw-w64-kicad-doc and mingw-w64-kicad-library when
# updating this build rule

_realname=kicad
_wx_basever=3.1
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.0.10
pkgrel=2
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.kicad.org/"
license=("spdx:GPL-3.0-or-later")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
options=('strip' '!debug') # Production build options
#options=('!strip' 'debug')
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
depends=(
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-glew"
  "${MINGW_PACKAGE_PREFIX}-glm"
  "${MINGW_PACKAGE_PREFIX}-ngspice"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-wxPython"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-freeglut"
  "${MINGW_PACKAGE_PREFIX}-zlib"
  $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-opencascade")
  "${MINGW_PACKAGE_PREFIX}-cairo"
  "${MINGW_PACKAGE_PREFIX}-kicad-footprints"
  "${MINGW_PACKAGE_PREFIX}-kicad-symbols"
)
makedepends=(
  "git"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-doxygen"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-swig"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw"
)
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-kicad-templates: for template libraries"
  "${MINGW_PACKAGE_PREFIX}-kicad-packages3D: for 3D model libraries"
)
_doc=("ca" "de" "en" "es" "fr" "id" "it" "ja" "nl" "pl" "ru" "zh")
for _doclang in ${_doc[@]}; do
  optdepends+=("${MINGW_PACKAGE_PREFIX}-${_realname}-doc-${_doclang}: for documentation (${_doclang})");
done
source=(
  "${_realname}-${pkgver}.tar.gz"::"https://gitlab.com/kicad/code/kicad/-/archive/${pkgver}/kicad-${pkgver}.tar.gz"
  '002-ki-6.0-cmake-fixes-for-MINGW-CLANG.patch'
  '003-ki-6.0-code-fixes-for-GNUC-CLANG.patch'
  '006-ki-6.0-rewrite-kiwin32_rc_for_clang.patch'
  '005-ki-6.0-Skip-ffloat-store-under-CLANG.patch'
  # This is not a patch to use with standard production builds
  '007-ki-6.0-manifest-remove-win10-11-manifest-support.patch'
)
sha256sums=('d73bedec7bc30549653ca508cc05070d4834e8d38805d0afde878a7f86a02db1'
            '45c672d20ae9badb9591f39cffc0191d6eb9a86ebbef53d43475954ffb89fe27'
            'd8d5f4bdd0aa6d8a907710c523f6f95840636cb2ef69e5275c6ed4966f134353'
            'e03dbb58409145c8fb54991d8259f14bc0ba6b21abca24f9b914ec354c9df89c'
            'f9cd1cbbafe98a76e0bd7cb61ab76b05b5a2c22848dd5ea8042c06e0c4864288'
            '550397b8d2ba66b924933020e409e868d3e4b2352a235f329798bc631b9fd28f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    002-ki-6.0-cmake-fixes-for-MINGW-CLANG.patch \
    003-ki-6.0-code-fixes-for-GNUC-CLANG.patch \
    005-ki-6.0-Skip-ffloat-store-under-CLANG.patch \
    006-ki-6.0-rewrite-kiwin32_rc_for_clang.patch
  # This patch is needed for old Intel GPU drivers and should not be
  # used for the production version of the package
  #apply_patch_with_msg \
    #007-ki-6.0-manifest-remove-win10-11-manifest-support.patch
}

build() {
  msg2 "Build KiCad"
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DCMAKE_PREFIX_PATH=;DEFAULT_INSTALL_PATH=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_PREFIX_PATH="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DDEFAULT_INSTALL_PATH="${MINGW_PREFIX}" \
      -DwxWidgets_CONFIG_EXECUTABLE="${MINGW_PREFIX}"/bin/wx-config-${_wx_basever} \
      -DKICAD_USE_OCC="$( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] && echo "OFF" || echo "ON" )" \
      -DOCC_INCLUDE_DIR="${MINGW_PREFIX}"/include/opencascade \
      -DKICAD_SCRIPTING_WXPYTHON=ON \
      -DKICAD_BUILD_I18N=ON \
      -DPYTHON_EXECUTABLE="${MINGW_PREFIX}"/bin/python.exe \
      -DPYTHON_INCLUDE_DIR=$("${MINGW_PREFIX}"/bin/python.exe -c "from sysconfig import get_paths as gp; print(gp()[\"include\"])") \
      -DPYTHON_ROOT_DIR="${MINGW_PREFIX}" \
      ../${_realname}-${pkgver}

  cd "${srcdir}"/build-${MSYSTEM}
  "${MINGW_PREFIX}"/bin/cmake.exe --build . # --parallel 1 --verbose
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install . # --parallel 1 --verbose

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.README" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.README"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.GPLv3" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.GPLv3"
}
